@extends('layouts.back_stage')
@section('content')
    <style>
        .express{width: 300px;margin-bottom: 30px;}
        .remarks{resize:none;width:300px;}
        .user_ul{border:1px dashed #999;width:800px;}
        .user_ul .user_li{padding:5px 20px;font-weight:bold;}
        table {border-collapse:collapse;}
        thead {display:block}
        tbody {width:100%;height:250px;overflow-y:scroll;display:block}
        .wt1{width:60px;}
        .wt2{width:220px;}
        .wt3,.wt4{width:150px;}
        .wt5,.wt6,.wt7,.wt8,.wt9,.wt10{width:130px;}
    </style>
    <script>
        var postages_price = [
                @foreach($postages_price as $k=>$v)
                    ['{{ $k }}','{{ $v[0] }}','{{ $v[1] }}'],
                @endforeach
//            [-1,'','']
        ];
    </script>
    <div class="container">
        <div style="padding:20px 0;overflow:hidden;">
            <h3 style="float: left;">用户信息</h3>
            <a href="javascript:;" class="btn btn-primary" style="float:right;" onclick="history.go(-1)">返回</a>
        </div>
        <ul class="user_ul">
            <input type="hidden" class="user_id" value="{{ $address_info->user_id }}">
            <li class="user_li">用户姓名：{{ $address_info->addressee_name }}</li>
            <li class="user_li">用户手机：{{ $address_info->addressee_tel }}</li>
            <li class="user_li">收货地址：{{ $address_info->addressee_place }}</li>
        </ul>
        <div style="padding:10px 0;"><h3 >订单信息</h3></div>
        <table class="table table-hover" >
            <thead>
            <tr>
                <td class="wt1">选择</td>
                <td class="wt2">购买时间</td>
                <td class="wt3">品牌</td>
                <td class="wt4">款号</td>
                <td class="wt5">数量/手</td>
                <td class="wt6">颜色</td>
                <td class="wt7">商品价格</td>
                <td class="wt8">邮费</td>
                <td class="wt9">订单总价</td>
                <td class="wt10">操作</td>
            </tr>
            </thead>
            <tbody>
            @foreach($orders as $k=>$v)
                <tr class="main_info">
                    <td class="wt1">
                        @if(!$type)
                            <input type="checkbox" name="inp" class="choose_inp" value="{{$v->id}}" abc="{{ $v['num'] }}" card="{{ $v['girard'] }}">
                        @endif
                    </td>
                    <td class="wt2">{{ $v['created_at'] }}</td>
                    <td class="wt3">{{ $brands[$v['brand_id']] }}</td>
                    <td class="wt4">{{ $v['girard'] }}</td>
                    <td class="wt5">{{ $v['num'] }}</td>
                    <td class="wt6">{{ $v['color'] }}</td>
                    <td class="wt7">{{ $v['total_price'] }}</td>
                    <td class="wt8">{{ $v['postage_estimate']}}</td>
                    <td class="wt9">{{ $v['total_price']+$v['postage_estimate'] }}</td>

                    @if($v['associated_at'])
                    <td class="wt10">已关联</td>
                    @else
                    <td class="wt10"></td>
                    @endif
                </tr>
            @endforeach
            </tbody>
        </table>
        <div style="padding:15px 0;"><h3 >填写快递单号信息</h3></div>
        <div style="padding-bottom:50px;">
            <div style="display: inline-block">
                <p style="margin-bottom:30px;">
                    <label>快递公司:</label>
                    <select name="express_company" id="sort" class="address express_company" style="width: 240px">
                        <option value="圆通快递" selected>圆通快递</option>
                        <option value="申通快递">申通快递</option>
                        <option value="中通快递">中通快递</option>
                        <option value="顺丰快递">顺丰快递</option>
                        <option value="百世汇通">百世汇通</option>
                        <option value="天天快递">天天快递</option>
                        <option value="韵达快递">韵达快递</option>
                        <option value="邮政包裹">邮政包裹</option>
                        <option value="营创物流">营创物流</option>
                    </select>
                </p>
                <label>运单号:</label><input class="form-control express_num express" type="text" value="{{ $express['express_num'] }}" onkeyup="clearNum(this)" >
                <label>备注:</label><textarea class="form-control remarks" rows="2" >{{ $express['remarks'] }}</textarea>
                @if(isset($express['express_company']) && $express['express_company'])
                <label>应收邮费:</label><input class="form-control express_price express" type="text" disabled value="{{$express['express_price']}}">
                @else
                <label>应收邮费:</label><input class="form-control express_price express" type="text" disabled value="0">
               @endif
            </div>
            @if(isset($express['express_company']) && $express['express_company'])
            <button style="display: block;" class="btn btn-info save_info" onclick="sureAmend()">保存</button>
            @else
            <input type="hidden" class="type" value="0">
            <button style="display: block;" class="btn btn-info" onclick="expressConfirm()">提交</button>
            @endif
        </div>
    </div>
    <script>
        @if(isset($_GET['see']))
            var see='{{ $_GET['see'] }}';
            if(see==1){
                $(".express_company").attr("disabled","disabled");
                $(".express_num").attr("disabled","disabled");
                $(".remarks").attr("disabled","disabled");
                $(".save_info").hide();
            }
        @endif
        var val='{{ $express['express_company'] }}';
        if(val!=''){
            $(".express_company").val(val);
        }
        var arr=[];
        /*邮费问题*/
        $(document).on('change','input[type=checkbox]',function () {
            var card=$(this).attr("card");
            var num = $(this).attr("abc");
            if($(this).prop('checked')){
                if(arr[card]){
                    arr[card] =  parseInt(arr[card])+parseInt(num) ;
                }else{
                    arr[card] = parseInt(num) ;
                }
            }else{
                if(arr[card]){
                    arr[card] =  parseInt(arr[card])-parseInt(num) ;
                }else{
                    arr[card] = parseInt(num) ;
                }
            }
            var postage = 0;
            for(var j in arr) {
                if(arr[j] < 3 && arr[j]>0)
                {
                    for(i in postages_price)
                    {
                        if(postages_price[i][0] == j)
                        {
                             var postage  =parseInt(postage)  + parseInt(postages_price[i][arr[j]]);
                        }
                    }
                }
            }
            $(".express_price").val(postage);
            for(var k in arr){
                if(arr[k]>=3){
                    $(".type").val("1");
                    return false;
                }else{
                    $(".type").val("0");
                }
            }
        });
        var page='{{ $_GET['page'] }}';
        function expressConfirm() {
            var type=$(".type").val();
            var express_company=$(".express_company").val();
            var express_num = $('.express_num').val();
            var remarks=$(".remarks").val();
            var express_price = $('.express_price').val();
            var user_id=$(".user_id").val();
            var s='';
            for(var i=0; i< $(".choose_inp").length;i++){
                if($(".choose_inp")[i].checked){
                    var id=$(".choose_inp")[i].value;
                    s=s+id+",";
                }
            }
            var ids=s.substr(0, s.length - 1);
            if(ids==''){
                $(".faceImg").attr("src","/img/weep.png");
                $(".oHandle").text("请选择订单");
                $(".oSuccess").show(0).delay(1000).hide(0);
                return false
            }
            if(express_num == ''){
                $(".faceImg").attr("src","/img/weep.png");
                $(".oHandle").text("请输入运单号");
                $(".oSuccess").show(0).delay(1000).hide(0);
                return false;
            }else if(express_num.length<10 || express_num.length>15){
                $(".faceImg").attr("src","/img/weep.png");
                $(".oHandle").text("运单号不正确");
                $(".oSuccess").show(0).delay(1000).hide(0);
                return false;
            }
            $.ajax({
                headers: {'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')},
                url:'/out-stage/expressDetail',
                type:'post',
                data:{'ids':ids,'express_num':express_num,'express_price':express_price,'user_id':user_id,'type':type,'express_company':express_company,'remarks':remarks},
                dataType:'json',
                success:function (data) {
                    if(data.success){
                        $(".faceImg").attr("src","/img/laugh.png");
                        $(".oHandle").text("提交成功");
                        $(".oSuccess").show(0).delay(1000).hide(0);
                        window.location.href = '/out-stage?page='+page;
                    }else {
                        $(".faceImg").attr("src","/img/weep.png");
                        $(".oHandle").text(data.msg);
                        $(".oSuccess").show(0).delay(1000).hide(0);
                    }

                }
            })
        }
        /*运单号*/
        function clearNum(obj){
            obj.value = obj.value.replace(/[^\d]/g,"");
        }
        /*价格*/
//            function clearNoNum(obj){
//                obj.value = obj.value.replace(/[^\d.]/g,"");
//                obj.value = obj.value.replace(/\.{2,}/g,".");
//                obj.value = obj.value.replace(/^\./g,"");
//                obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
//                obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3');
//                if(obj.value.indexOf(".")< 0 && obj.value !=""){
//                    obj.value= parseFloat(obj.value);
//                }
//            }
        /*修改*/
        function sureAmend(){
            var order_id='{{ $_GET['id'] }}';
            var express_company = $('.express_company').val();
            var express_num = $('.express_num').val();
            var remarks=$(".remarks").val();
            var express_price = $('.express_price').val();
            if(express_num==''){
                $(".faceImg").attr("src","/img/weep.png");
                $(".oHandle").text("请输入运单号");
                $(".oSuccess").show(0).delay(1000).hide(0);
                return false;
            }else if(express_num.length<10 || express_num.length>15){
                $(".faceImg").attr("src","/img/weep.png");
                $(".oHandle").text("运单号不正确");
                $(".oSuccess").show(0).delay(1000).hide(0);
                return false;
            }
            $.ajax({
                headers: {'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')},
                url:'/out-stage/updateExpress',
                type:'post',
                data:{'order_id':order_id,'express_num':express_num,'express_price':express_price,'express_company':express_company,'remarks':remarks},
                dataType:'json',
                success:function (data) {
                    if(data.success){
                        $(".faceImg").attr("src","/img/laugh.png");
                        $(".oHandle").text("修改成功");
                        $(".oSuccess").show(0).delay(1000).hide(0);
                        window.location.href = '/out-stage?page='+page;
                    }else {
                        $(".faceImg").attr("src","/img/weep.png");
                        $(".oHandle").text(data.msg);
                        $(".oSuccess").show(0).delay(1000).hide(0);
                    }

                }
            })
        }
    </script>
@endsection
